// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, { soundList } from "../../framework/AudioManage";
import GameConfig from "../../framework/GameConfig";
import mainManage from "../../framework/mainManage";
import sceneMediator from "../../framework/sceneMediator";
import Tool from "../../framework/Tool";
import gameData from "../gameData";
import TTSDK from "../../framework/sdk/TTSDK";
import toastMediator from "../../framework/toastMediator";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameUIMediator extends cc.Component {

    @property(cc.Label)
    pumpkinNumber: cc.Label = null;

    @property(cc.Node)
    pauseView: cc.Node = null;

    @property(cc.Node)
    titleView: cc.Node = null;

    @property(cc.Label)
    gameTitle: cc.Label = null;

    enterTimes = 0;

    timeCount = 0;

    isEndless = false;

    endlessTitle = "";

    lastIndex = -1;

    onLoad() {

        this.setPumpkinButtonStatus();

        this.updatePumpkinNumber();

        this.updateTitleLabel();

        if (gameData.failGameData >= 3) {
            this.setTitleAction(0);
        } else {
            this.setTitleAction(1);
        }

        this.registerEvent();

        this.setGameTitlePosition();
    }

    //设置UI界面苹果是否为按钮 触发碎片功能
    setPumpkinButtonStatus(){
        this.pumpkinNumber.node.parent.getChildByName("apple").active = true;
        this.pumpkinNumber.node.parent.getChildByName("skin").active = false;
    }

    //设置UI界面 提示的显示
    updateTitleLabel(){
        this.titleView.getChildByName("bg").getChildByName("showLab").getComponent(cc.Label).string = gameData.getCurTitleString()[sceneMediator.getShowScene() - 1] + "";

        if (sceneMediator.getShowScene() > 1) {
            if (sceneMediator.getShowScene() == 42 || sceneMediator.getShowScene() == 12 || sceneMediator.getShowScene() == 24 || sceneMediator.getShowScene() == 34) {
                this.gameTitle.string = "";
            } else {
                this.gameTitle.string = gameData.getTitleData(sceneMediator.getShowScene(), gameData.getCurTitle()) + "";
            }
        } else {
            this.gameTitle.string = "";
        }
    }

    //设置自定义提示的位置
    setGameTitlePosition() {
        if (sceneMediator.getShowScene() == 3) {
            this.gameTitle.node.y = 160;
        } else if (sceneMediator.getShowScene() == 4) {
            this.gameTitle.node.y = 120;
        } else if (sceneMediator.getShowScene() == 37) {
            this.gameTitle.node.y = 195;
        } else if (sceneMediator.getShowScene() == 14) {
            this.gameTitle.node.y = 160;
        } else if (sceneMediator.getShowScene() == 10) {
            this.gameTitle.node.y = 160;
        } else if (sceneMediator.getShowScene() == 44) {
            this.gameTitle.node.y = 120;
        } else if (sceneMediator.getShowScene() == 21) {
            this.gameTitle.node.y = 50;
        } else if (sceneMediator.getShowScene() == 26) {
            this.gameTitle.node.y = 150;
        } else if (sceneMediator.getShowScene() == 27) {
            this.gameTitle.node.y = 50;
        } else if (sceneMediator.getShowScene() == 36) {
            this.gameTitle.node.y = 150;
        }
    }

    //设置提示的动作状态
    setTitleAction(status) {

        let hide = this.node.getChildByName("hide");
        let title = hide.getChildByName("title");
        let hand = hide.getChildByName("hand");
        let pic = hand.getChildByName("pic");

        if (status == 0) {

            cc.tween(pic)
                .repeatForever(
                    cc.tween(pic)
                        .by(0.3, {y: 20})
                        .by(0.3, {y: -20})
                        .start()
                )
                .start();

            hand.active = true;
            cc.tween(title)
                .repeatForever(
                    cc.tween(title)
                        .to(0.1, {scale: 1.2})
                        .to(0.1, {scale: 1})
                        .start()
                )
                .start();
        } else {
            title.stopAllActions();
            title.scale = 1;
            hand.active = false;
            pic.sortAllChildren();
            pic.y = 0;
        }
    }

    //更新苹果的数量
    updatePumpkinNumber(){
        if (sceneMediator.curEnterLevel == 0) {
            this.pumpkinNumber.string = gameData.getProcessPumpkin()[sceneMediator.getShowScene() - 1] + " / " + gameData.getLevelPumpkin()[sceneMediator.getShowScene() - 1];
        } else {
            this.pumpkinNumber.string = gameData.getProcessPumpkin()[sceneMediator.princeShowLevel - 1 + 50] + " / " + gameData.getLevelPumpkin()[sceneMediator.princeShowLevel - 1 + 50];
        }
    }

    setCallBack(endless, endlessTitle) {
        this.isEndless = endless;
        this.endlessTitle = endlessTitle;
    }

    protected onDestroy(): void {
        cc.game.off("addPumpkin");
        cc.game.off("setTitle");
        cc.game.off("closePauseView");
        cc.game.off("startShowCheats");
        cc.game.off("CloseShowCheats");
        this.unschedule(this.timeAddAction);
    }

    titleAction = false;
    JumpAction = false;

    timeAddAction() {
        this.timeCount++;
        if (this.timeCount >= 40) {
            if (this.JumpAction) return;
            this.JumpAction = true;
            this.jumpBtnScale();
        } else if (this.timeCount >= 20) {
            if (this.titleAction) return;
            this.titleAction = true;
            this.titleBtnScale();
        }
    }

    //提示按钮缩放
    titleBtnScale() {
        cc.tween(this.node.getChildByName("hide").getChildByName("title")).repeat(2,
            cc.tween(this.node.getChildByName("hide").getChildByName("title")).to(0.2, {
                scale: 1.2
            }).call(() => {
                cc.tween(this.node.getChildByName("hide").getChildByName("title")).to(0.2, {
                    scale: 1
                }).start();
            }).start()
        ).call(() => {
            cc.tween(this.node.getChildByName("hide").getChildByName("title")).delay(1.5).call(() => {
                if (this.JumpAction) return;
                this.titleBtnScale();
            }).start();
        }).start();
    }

    //跳过按钮缩放
    jumpBtnScale() {
        cc.tween(this.node.getChildByName("hide").getChildByName("jump")).repeat(2,
            cc.tween(this.node.getChildByName("hide").getChildByName("jump")).to(0.2, {
                scale: 1.2
            }).call(() => {
                cc.tween(this.node.getChildByName("hide").getChildByName("jump")).to(0.2, {
                    scale: 1
                }).start();
            }).start()
        ).call(() => {
            cc.tween(this.node.getChildByName("hide").getChildByName("jump")).delay(1.5).call(() => {
                this.jumpBtnScale();
            }).start();
        }).start();
    }

    //彩蛋按钮
    EggBtnScale() {
        cc.tween(this.node.getChildByName("hide").getChildByName("EasterEgg")).to(0.2, {
            scale: 1.35
        }).call(() => {
            cc.tween(this.node.getChildByName("hide").getChildByName("EasterEgg")).to(0.2, {
                scale: 1.1
            }).call(() => {
                cc.tween(this.node.getChildByName("hide").getChildByName("EasterEgg")).delay(0.3).call(() => {
                    this.EggBtnScale();
                }).start();
            }).start();
        }).start()
    }

    registerEvent() {

        //按下按键的监听事件
        cc.game.on("addPumpkin", () => {
            gameData.addPumpkin();
            if (sceneMediator.curEnterLevel == 0) {
                this.pumpkinNumber.string = gameData.getProcessPumpkin()[sceneMediator.getShowScene() - 1] + " / " + gameData.getLevelPumpkin()[sceneMediator.getShowScene() - 1];
            } else {
                this.pumpkinNumber.string = gameData.getProcessPumpkin()[50 + sceneMediator.princeShowLevel - 1] + " / " + gameData.getLevelPumpkin()[50 + sceneMediator.princeShowLevel - 1];
            }

        });

        cc.game.on("setTitle", (index?) => {
            if (index) {
                if (this.lastIndex != index[0]) {
                    console.log(this.lastIndex, index[0]);
                    this.lastIndex = index[0];
                    this.gameTitle.string = gameData.getTitleData(sceneMediator.getShowScene(), index[0]);
                }
            } else {
                this.gameTitle.string = gameData.getTitleData(sceneMediator.getShowScene(), gameData.getCurTitle());
            }
        });

        cc.game.on("closePauseView", () => {
            this.pauseView.active = false;
        });
    }

    onClick(event, data) {
        AudioManage.playSFX(soundList.uiClick, false);
        if (parseInt(data) == 1) {
            if (sceneMediator.getShowScene() == 22) {

                cc.find("Canvas/keyGroup").active = false;
                let key_hight = cc.find("Canvas/keyGroup_high");
                key_hight.active = true;
                key_hight.parent = cc.find("Canvas/gameUI/pauseView");
                key_hight.setPosition(cc.v2(0,0));
                key_hight.setSiblingIndex(1);

                // this.pauseView.getComponent(cc.BlockInputEvents).enabled = false;
                cc.game.emit("clickPause");
            } else {
                cc.director.getPhysicsManager().enabled = false;
            }
            this.pauseView.active = true;
            console.log("暂停");
        }

        if (parseInt(data) == 2) {

            cc.director.getPhysicsManager().enabled = false;

            TTSDK.Instance.showRewardVideo({
                success:()=>{
                    if(GameConfig.GetInstance().isLockHide){
                        this.setTitleAction(1);
                        this.titleView.active = true;
                        return;
                    }

                    if (sceneMediator.curEnterLevel == 1) {

                        this.titleView.active = true;

                    } else if (sceneMediator.curEnterLevel == 0 && sceneMediator.getShowScene() != 7) {

                        this.setTitleAction(1);
                        this.titleView.active = true;

                    }else if(sceneMediator.curEnterLevel == 0 && sceneMediator.getShowScene() == 7){
                        console.log("第7关");
                        this.titleView.active = true;
                    }
                },
                fail :()=>{
                    cc.director.getPhysicsManager().enabled = true;
                    mainManage.createToast("看完视频才能获得提示哦!");
                }
            });
        }

        if (parseInt(data) == 3) {
            cc.director.getPhysicsManager().enabled = false;

            TTSDK.Instance.showRewardVideo({
                    success:()=>{
                        sceneMediator.jumpNextScene();
                    },
                    fail :()=>{
                        cc.director.getPhysicsManager().enabled = true;
                        mainManage.createToast("看完视频在跳过吧!");
                    }
            });
            console.log("跳过");
        }

        if (parseInt(data) == 4) {
            if (sceneMediator.getShowScene() == 22) {
                cc.find("Canvas/keyGroup").active = true;
                let key_hight = cc.find("Canvas/gameUI/pauseView/keyGroup_high");
                key_hight.parent = cc.find("Canvas");
                key_hight.setPosition(cc.v2(0,0));
                key_hight.setSiblingIndex(14);
                key_hight.active = false;
                cc.game.emit("clickPause");
            } else {
                cc.director.getPhysicsManager().enabled = true;
            }
            this.pauseView.active = false;
            console.log("恢复暂停");
        }

        if (parseInt(data) == 5) {

            sceneMediator.backToHome();
        }

        if (parseInt(data) == 6) {
            this.titleView.active = false;
            cc.director.getPhysicsManager().enabled = true;
            cc.game.emit("clickTitle");
            cc.game.emit("showTitleEvent");
        }
    }
}
